import os

from Config import *

class CategoryHandler:
    def __init__(self):
        self.__cache = {}
        self.__readCategoryConf()

    @property
    def CategoryPairs(self):
        return self.__cache

    def ExtendPairs(self, extendFilePath, newConfPath):
        with open(extendFilePath, encoding = ENCODING) as f:
            for l in f.readlines():
                items = l.split(SEPARATOR)
                if items[5].strip() == "TRUE" or items[5].strip() == "FALSE" or items[5].strip() == "Column6":
                    continue
                desc = items[7].strip()
                category = items[4].strip()
                if desc in self.CategoryPairs:
                    print(desc + " already exists!")
                self.CategoryPairs[desc] = category
                print("Add " + desc + ": " + category)
        with open(newConfPath, "w", encoding = ENCODING) as f:
            for desc in sorted(self.CategoryPairs):
                f.write(desc + SEPARATOR + self.CategoryPairs[desc] + "\n")

    def __readCategoryConf(self):
        confPath = os.path.join(os.path.dirname(__file__), CONF_NAME)
        with open(confPath, encoding = ENCODING) as f:
            for l in f.readlines():
                pair = l.split(SEPARATOR)
                self.__cache[pair[0].strip()] = pair[1].strip()

